package com.warom.sdg.websocket;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

/**
 * WebSocket消息实体类
 * 用于封装WebSocket通信的消息格式
 */
public class WebSocketMessage {
    
    // 消息类型
    @SerializedName("type")
    private String type;
    
    // 命令类型（当type为command时使用）
    @SerializedName("command")
    private String command;
    
    // 消息内容
    @SerializedName("content")
    private String content;
    
    // 英文名称（设备标识）
    @SerializedName("englishName")
    private String englishName;
    
    // 消息接收者ID
    @SerializedName("receiver")
    private String receiver;
    
    // 消息发送者ID
    @SerializedName("sender")
    private String sender;
    
    // 消息时间戳
    @SerializedName("timestamp")
    private long timestamp;
    
    // 通知标题（当type为notification时使用）
    @SerializedName("title")
    private String title;
    
    // 通知消息（当type为notification时使用）
    @SerializedName("message")
    private String message;
    
    /**
     * 默认构造函数
     */
    public WebSocketMessage() {
        this.timestamp = System.currentTimeMillis();
    }
    
    /**
     * 构造命令消息
     * 
     * @param command 命令名称
     * @param content 命令内容
     */
    public WebSocketMessage(String command, String content) {
        this();
        this.type = "command";
        this.command = command;
        this.content = content;
    }
    
//    /**
//     * 构造设备控制消息
//     *
//     * @param englishName 设备英文名称
//     * @param content 控制参数
//     * @param toUserId 接收者ID
//     */
//    public WebSocketMessage(String englishName, String content, String toUserId) {
//        this();
//        this.type = "command";
//        this.command = "controlSingle";
//        this.englishName = englishName;
//        this.content = content;
//        this.toUserId = toUserId;
//    }
//
//    /**
//     * 构造通知消息
//     *
//     * @param title 通知标题
//     * @param message 通知内容
//     * @param content 通知数据
//     */
//    public WebSocketMessage(String title, String message, String content) {
//        this();
//        this.type = "notification";
//        this.title = title;
//        this.message = message;
//        this.content = content;
//    }
    
    /**
     * 从JSON字符串创建消息对象
     * 
     * @param json JSON字符串
     * @return WebSocketMessage对象
     */
    public static WebSocketMessage fromJson(String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, WebSocketMessage.class);
    }
    
    /**
     * 将消息对象转换为JSON字符串
     * 
     * @return JSON字符串
     */
    public String toJson() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }
    

    
    /**
     * 创建心跳消息
     * 
     * @return WebSocketMessage对象
     */
    public static WebSocketMessage createHeartbeatMessage() {
        WebSocketMessage message = new WebSocketMessage();
        message.setType("heartbeat");
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }
    
    // Getter和Setter方法
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getCommand() {
        return command;
    }
    
    public void setCommand(String command) {
        this.command = command;
    }
    
    public String getContent() {
        return content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public String getEnglishName() {
        return englishName;
    }
    
    public void setEnglishName(String englishName) {
        this.englishName = englishName;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
}
